{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import faiss \n",
    "import torch\n",
    "import numpy as np\n",
    "from tqdm import trange\n",
    "from sentence_transformers import SentenceTransformer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from gc import collect\n",
    "\n",
    "def clear_cuda_cache():\n",
    "    collect()  # 强制系统垃圾回收\n",
    "    torch.cuda.empty_cache()  # 清空PyTorch缓存\n",
    "    # 查看释放效果（可选）\n",
    "    print(f\"释放后可用显存：{torch.cuda.mem_get_info()[0]/1024**3:.2f} GB\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "释放后可用显存：21.51 GB\n"
     ]
    }
   ],
   "source": [
    "clear_cuda_cache()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load the Embedding Model\n",
    "### 加载embedding模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<All keys matched successfully>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "SentenceTransformer(\n",
       "  (0): Transformer({'max_seq_length': 8192, 'do_lower_case': False}) with Transformer model: NomicBertModel \n",
       "  (1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False, 'include_prompt': True})\n",
       "  (2): Normalize()\n",
       ")"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "embedding_model = \"./embedding_models/AI-ModelScope/nomic-embed-text-v1/\"\n",
    "embedding_model = SentenceTransformer(embedding_model, trust_remote_code=True)\n",
    "\n",
    "embedding_model.to(torch.device('cuda'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read the pre-prepared JSON file in the format required by TinyDB (refer to arxiv_paper_db.json for the specific format). Each index corresponds to a single paper.\n",
    "### 读取准备好符合tinydb要求json文件（具体格式参考arxiv_paper_db.json）, 每个索引index对应一篇paper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('./database/arxiv_paper_db.json','r') as f:\n",
    "    papers = json.loads(f.read())\n",
    "papers_l = list(papers['cs_paper_info'].items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('1',\n",
       "  {'id': '1',\n",
       "   'title': '优质护理对疝气手术患者术后的效果观察及对心理状态的影响分析',\n",
       "   'abs': '目的：探究围手术期护理中采用优质护理在疝气术后患者中的应用效果及对心理状态的影响。方法：本次研究对象是2021年1月—2023年12月期间来我院接受手术治疗的56例疝气患者，根据护理模式的差异性将其分为接受常规护理的28例对照组和接受优质护理的28例观察组，通过比较两组患者的并发症发生率和心理状态变化来分析优质护理的应用价值。结果：观察组患者围手术期的压疮、恶心呕吐、肺部感染以及阴囊血肿等并发症发生率明显低于对照组（P<0.05）。干预前，两组患者的SDS及SAS评分比较差异不具有统计学意义（P>0.05）；干预后，观察组患者的SDS及SAS评分均低于对照组，差异具有统计学意（P<0.05）。结论：在疝气手术患者的围手术期护理中采用优质护理，可明显降低术后并发症发生率，改善其心理状态。',\n",
       "   'authors': ['杨颖'],\n",
       "   'date': '2024-12-30',\n",
       "   'url': '',\n",
       "   'cat': ''}),\n",
       " ('2',\n",
       "  {'id': '2',\n",
       "   'title': '结构式团体心理干预在疝气手术患者心理弹性及依从性中的效果分析',\n",
       "   'abs': '目的 分析结构式团体心理干预在疝气手术中的具体优势。方法 纳入疝气手术患者80例，于2021年1月～2023年12月入院，按照数字表法随机分组，对照组（n=40）、观察组（n=40），对照组采用常规护理，观察组在对照组的基础上联合结构式团体心理干预。两组均干预1个月，于干预前、干预1个月后比较两组患者的心理弹性、不良情绪、病耻感和应对方式；于干预1个月后，比较两组患者的依从性。结果 干预1个月后，观察组成人心理弹性量表（RSA）评分显著高于对照组（P<0.05）。干预1个月后，观察组自我感受负担量表（SPB）评分、抑郁-焦虑-应激自评量表（DASS-21）评分量表中压力（DASS-S）、抑郁（DASS-D）、焦虑（DASS-A）中的评分均显著低于对照组（P<0.05）。干预1个月后，观察组Link病耻感量表评分中贬低-歧视感知、应对方式、情感体验的评分显著低于对照组（P<0.05），观察组创伤后成长评定量表（PTGI）评分较对照组显著增高（P<0.05）。干预1个月后，两组依从性对比有差异，其中观察组更高（P<0.05）。结论 结构式团体心理干预在疝气手术中有显著优势，可明确改善心理弹性和不良情绪，有效缓解病耻感，促使其以积极的态度面对疾病，有利于改善依从性。',\n",
       "   'authors': ['王玉丽', '谢晶晶', '刘丽'],\n",
       "   'date': '2024-12-28',\n",
       "   'url': '',\n",
       "   'cat': ''}),\n",
       " ('3',\n",
       "  {'id': '3',\n",
       "   'title': '腹腔镜无张力疝修补手术治疗成人疝气的临床效果',\n",
       "   'abs': '目的 以本院择期采取手术治疗的成人疝气患者为试验样本，评价腹腔镜无张力疝修补手术的临床应用效果。方法 选取2021年1月至2023年12月我院采取手术治疗的成人疝气患者66例，按照术式不同分为两组，对照组15例、观察组51例，前者应用传统无张力疝修补术，后者所用术式为腹腔镜无张力疝修补术，对比围手术期指标参数，术后并发症发生率，手术前后疼痛评分（VAS评分）、术后复发率。结果 观察组各项围手术期指标低于对照组（P <0.05）。观察组并发症发生率低于对照组（P <0.05）。术后，观察组VAS评分低于对照组（P <0.05）。观察组术后6个月、12个月复发率低于对照组，但两组对比无统计学意义（P> 0.05）。结论 成人疝气治疗期间选用腹腔镜无张力疝修补手术对于减轻临床症状效果理想，和传统无张力修补术相比安全性较高，可促进患者及早康复。',\n",
       "   'authors': ['余德生'],\n",
       "   'date': '2024-12-23',\n",
       "   'url': '',\n",
       "   'cat': ''}),\n",
       " ('4',\n",
       "  {'id': '4',\n",
       "   'title': 'Title-题名',\n",
       "   'abs': 'Summary-摘要',\n",
       "   'authors': ['Author-作者'],\n",
       "   'date': 'PubTime-发表时间',\n",
       "   'url': '',\n",
       "   'cat': ''}),\n",
       " ('5',\n",
       "  {'id': '5',\n",
       "   'title': '不同方式无张力疝修补术治疗腹股沟疝气的临床疗效及复发情况比较',\n",
       "   'abs': '目的:对比传统无张力疝修补术与开放完全腹膜外腹股沟疝修补术(TEP)在治疗腹股沟疝气中的疗效及复发情况,为临床提供治疗参考。方法:选择我院普外科收治的腹股沟疝患者,分为对照组和观察组。对照组采用传统无张力疝修补术,观察组则采用开放TEP。通过对比两组患者的手术时间、住院时间、住院费用、并发症发生率及复发率,全面评估两种术式的临床效果。结果:经过严格的数据分析,我们发现观察组患者的手术时间、术后住院时间均短于对照组,显示出开放TEP在手术效率方面的优势。虽然观察组的住院费用相对较高,但其在降低并发症发生率方面表现出显著效果。在复发情况方面,两种术式均表现出较低的复发率,但开放TEP的复发风险相对更低。结论:传统无张力疝修补术和开放TEP都是治疗腹股沟疝的有效方法,具有较低的复发率。相比之下,开放TEP在手术时间、术后恢复及并发症控制方面更具优势,但住院费用较高。因此,在选择手术方式时,医生应根据患者的经济承受能力和具体病情进行综合考虑。随着医疗技术的不断进步,我们相信未来会有更多优秀的治疗方法涌现,为腹股沟疝患者带来更好的治疗体验和生活质量。总之,本研究通过对比分析不同方式无张力疝修补术治疗腹股沟疝气的临床疗效及复发情况,为临床医生提供了宝贵的治疗参考。在未来的临床实践中,我们应继续探索和创新,以期为患者提供更加精准、高效的治疗方案。',\n",
       "   'authors': ['张彦林'],\n",
       "   'date': '2024-12-21',\n",
       "   'url': '',\n",
       "   'cat': ''}),\n",
       " ('6',\n",
       "  {'id': '6',\n",
       "   'title': '微创手术治疗小儿疝气在围术期实施心理护理的研究',\n",
       "   'abs': '目的:明确心理护理在小儿疝气患者接受微创手术治疗围术期的应用价值。方法:以2023.01-12时间段中于院内接受微创手术治疗小儿疝气患者为分析对象,共计95例,对各个患者进行编号后采取随机抽取法设置成对照组(49例)、观察组(46例),分别以常规手段、心理护理手段实施两组护理干预,对干预情况进行分析。结果:应激反应方面,两组相比,观察组各项值更低(P<0.05)。临床指标方面,两组相比,观察组各项值更低(P<0.05)。并发症发生情况方面,两组相比,观察组总并发症例数占比更少(P<0.05)。结论:在微创手术治疗小儿疝气在围术期实施心理护理有利于减轻患儿因不良情绪引起的应激反应,减少患儿哭闹时间与临床症状持续时长,可降低相关并发症发生风险,让患儿尽早出院。',\n",
       "   'authors': ['田雅丽'],\n",
       "   'date': '2024-12-21',\n",
       "   'url': '',\n",
       "   'cat': ''}),\n",
       " ('7',\n",
       "  {'id': '7',\n",
       "   'title': '循证的临床护理路径对小儿疝气患者术后疼痛缓解的效果',\n",
       "   'abs': '目的:分析小儿疝气患者采取循证的临床护理路径对术后疼痛缓解作用。方法:择取本院手术治疗小儿疝气患者共84例(时段2022年11月至2023年11月),将其均分两组,各组42例。对照组开展常规护理,观察组开展循证的临床护理路径。评比两组患患儿术后疼痛程度;评估患儿治疗依从性及负性情绪改善情况;记录术后并发症情况。结果:对比术后24h、48h、72h面部表情疼痛量表(FPS),观察组与对照组比较均显著降低,两组存在统计学意义(P<0.05)。两组患者干预后儿童焦虑性情绪障碍筛查量表(SCARED)评分较干预前均减少(P<0.05);观察组SCARED评分相比对照组更低(P<0.05)。对比治疗依从性评估结果,观察组总依从率相比对照组更高(P<0.05)。对比术后并发症情况,观察组总发生率相比对照组更高低(P<0.05)。结论:小儿疝气患者采取循证的临床护理路径对术后疼痛有明显缓解作用,并有助于改善负性情绪,提高治疗依从性,降低术后并发症风险。',\n",
       "   'authors': ['苏越'],\n",
       "   'date': '2024-12-21',\n",
       "   'url': '',\n",
       "   'cat': ''}),\n",
       " ('8',\n",
       "  {'id': '8',\n",
       "   'title': '综合护理在腹腔镜下小儿疝气手术中的应用效果',\n",
       "   'abs': '目的:分析腹腔镜下小儿疝气手术用综合护理所起到的作用。方法:随机均分2022年1月-2024年10月本院接诊腹腔镜下疝气手术患儿(n=52)。试验组采取综合护理,对照组行常规护理。对比依从性等指标。结果:关于依从性:试验组96.15%,对照组84.62%,差异显著(P<0.05)。住院时间和下床活动时间:试验组短于对照组(P<0.05)。家属满意度:试验组96.15%,对照组80.77%,差异显著(P<0.05)。并发症:试验组3.85%,对照组19.23%,差异显著(P<0.05)。结论:腹腔镜下小儿疝气手术用综合护理,患儿的依从性更高,病情康复情况更好,并发症也更少,家属满意度改善更加显著。',\n",
       "   'authors': ['谢素婷'],\n",
       "   'date': '2024-12-21',\n",
       "   'url': '',\n",
       "   'cat': ''}),\n",
       " ('9',\n",
       "  {'id': '9',\n",
       "   'title': 'Title-题名',\n",
       "   'abs': 'Summary-摘要',\n",
       "   'authors': ['Author-作者'],\n",
       "   'date': 'PubTime-发表时间',\n",
       "   'url': '',\n",
       "   'cat': ''}),\n",
       " ('10',\n",
       "  {'id': '10',\n",
       "   'title': '人体“疝”事知多少',\n",
       "   'abs': '<正>疝气是指人体组织或者器官一部分离开了原来的位置,通过人体间隙、缺损或者薄弱部位,进入另一部分,俗称“小肠串气”。可根据其发生的部位、诱因等来进行分类,有脐疝、腹股沟直疝、斜疝、切口疝、手术复发疝、白线疝、股疝等。在临床实践中发现,咳嗽、喷嚏、用力过度、腹部过肥、排便过于用力、老年腹壁强度退行性变化等都可能会引起疝气。',\n",
       "   'authors': ['顾梅丽', '黄梅', '卢颜冰'],\n",
       "   'date': '2024-12-20',\n",
       "   'url': '',\n",
       "   'cat': ''})]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "papers_l[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get embeddings of abs and title\n",
    "## 对title和abs做embedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "from tqdm import trange\n",
    "\n",
    "def get_embeddings(text_l, batch_size=32):\n",
    "    \"\"\"返回二维numpy数组，形状为 (n_samples, embedding_dim)\"\"\"\n",
    "    res = []\n",
    "    for i in trange(0, len(text_l), batch_size):\n",
    "        batch_text = ['search_document: ' + _ for _ in text_l[i:i+batch_size]]\n",
    "        # 确保返回numpy数组\n",
    "        batch_emb = embedding_model.encode(batch_text)\n",
    "        res.append(np.asarray(batch_emb, dtype='float32'))  # 强制转换为float32\n",
    "    return np.vstack(res)  # 垂直堆叠保证二维结构\n",
    "\n",
    "def batch_get_embeddings(text_list, batch_size=50):\n",
    "    \"\"\"返回形状为 (n_total_samples, embedding_dim) 的numpy数组\"\"\"\n",
    "    embeddings = []\n",
    "    for i in range(0, len(text_list), batch_size):\n",
    "        batch = text_list[i:i+batch_size]\n",
    "        \n",
    "        # 处理单个批次（得到二维数组）\n",
    "        batch_emb = get_embeddings(batch)\n",
    "        \n",
    "        # 追加整个批次（保持二维结构）\n",
    "        embeddings.append(batch_emb)  # 使用append而不是extend\n",
    "        \n",
    "        # 显存清理\n",
    "        if isinstance(batch_emb, np.ndarray):\n",
    "            del batch_emb\n",
    "        elif torch.is_tensor(batch_emb):\n",
    "            del batch_emb\n",
    "            torch.cuda.empty_cache()\n",
    "    \n",
    "    # 垂直堆叠所有批次\n",
    "    return np.vstack(embeddings).astype('float32')\n",
    "\n",
    "def adaptive_batch_get_embeddings(text_list, init_batch_size=8):\n",
    "    \"\"\"返回形状为 (n_total_samples, embedding_dim) 的numpy数组\"\"\"\n",
    "    batch_size = init_batch_size\n",
    "    total = len(text_list)\n",
    "    embeddings = []\n",
    "    pointer = 0\n",
    "    \n",
    "    while pointer < total:\n",
    "        try:\n",
    "            batch = text_list[pointer:pointer+batch_size]\n",
    "            # 处理当前批次（得到二维数组）\n",
    "            batch_emb = get_embeddings(batch)\n",
    "            \n",
    "            # 追加整个批次（保持二维结构）\n",
    "            embeddings.append(batch_emb)\n",
    "            pointer += batch_size\n",
    "            \n",
    "            # 成功时逐步增加批次（指数回撤策略）\n",
    "            next_batch_size = min(batch_size * 2, 64)\n",
    "            if (pointer + next_batch_size) > total:\n",
    "                next_batch_size = total - pointer\n",
    "            batch_size = next_batch_size\n",
    "\n",
    "        except RuntimeError as e:\n",
    "            if 'CUDA out of memory' in str(e):\n",
    "                # 显存清理\n",
    "                if 'torch' in globals():\n",
    "                    torch.cuda.empty_cache()\n",
    "                # 回撤策略：回退指针并缩小批次\n",
    "                batch_size = max(batch_size // 2, 1)\n",
    "                print(f\"OOM触发，回撤到位置 {pointer}，新批次大小 {batch_size}\")\n",
    "                # 如果批次过小仍失败，终止循环\n",
    "                if batch_size < 1:\n",
    "                    raise\n",
    "            else:\n",
    "                raise\n",
    "    \n",
    "    # 合并所有批次并确保数据类型\n",
    "    return np.vstack(embeddings).astype('float32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "title_l = [paper[1]['title'] for paper in papers_l]\n",
    "abs_l = [paper[1]['abs'] for paper in papers_l]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "释放后可用显存：22.60 GB\n"
     ]
    }
   ],
   "source": [
    "clear_cuda_cache()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00,  2.80it/s]\n",
      "100%|██████████| 1/1 [00:00<00:00, 82.71it/s]\n",
      "100%|██████████| 1/1 [00:00<00:00, 59.77it/s]\n",
      "100%|██████████| 2/2 [00:00<00:00, 64.50it/s]\n",
      "100%|██████████| 2/2 [00:00<00:00, 63.54it/s]\n",
      "100%|██████████| 2/2 [00:00<00:00, 70.26it/s]\n",
      "100%|██████████| 2/2 [00:00<00:00, 62.87it/s]\n",
      "100%|██████████| 2/2 [00:00<00:00, 62.50it/s]\n",
      "100%|██████████| 2/2 [00:00<00:00, 68.97it/s]\n"
     ]
    }
   ],
   "source": [
    "title_embeddings = adaptive_batch_get_embeddings(title_l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "释放后可用显存：22.51 GB\n"
     ]
    }
   ],
   "source": [
    "clear_cuda_cache()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00, 19.06it/s]\n",
      "100%|██████████| 1/1 [00:00<00:00,  5.49it/s]\n",
      "100%|██████████| 1/1 [00:00<00:00,  4.94it/s]\n",
      " 50%|█████     | 1/2 [00:00<00:00,  1.54it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OOM触发，回撤到位置 56，新批次大小 32\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00,  2.72it/s]\n",
      "  0%|          | 0/2 [00:00<?, ?it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OOM触发，回撤到位置 88，新批次大小 32\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/1 [00:00<?, ?it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OOM触发，回撤到位置 88，新批次大小 16\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00,  1.31it/s]\n",
      "  0%|          | 0/1 [00:00<?, ?it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OOM触发，回撤到位置 104，新批次大小 16\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00,  1.30it/s]\n",
      "100%|██████████| 1/1 [00:00<00:00,  4.01it/s]\n",
      "100%|██████████| 2/2 [00:00<00:00,  2.35it/s]\n",
      "100%|██████████| 2/2 [00:01<00:00,  1.30it/s]\n",
      "100%|██████████| 2/2 [00:00<00:00,  2.56it/s]\n",
      "100%|██████████| 2/2 [00:01<00:00,  1.51it/s]\n",
      "100%|██████████| 1/1 [00:00<00:00, 37.60it/s]\n"
     ]
    }
   ],
   "source": [
    "abs_embeddings = adaptive_batch_get_embeddings(abs_l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Convert embeddings into faiss-index\n",
    "### 将向量储存为faiss index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "title_index = faiss.IndexFlatL2(title_embeddings.shape[1])\n",
    "title_index.add(title_embeddings)\n",
    "\n",
    "abs_index = faiss.IndexFlatL2(abs_embeddings.shape[1])\n",
    "abs_index.add(abs_embeddings)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Save faiss-index, replacing the .bin file in the database folder.\n",
    "### 向量保存到本地，替换掉database文件夹中的.bin文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "faiss.write_index(faiss.index_gpu_to_cpu(title_index), 'titles.index')\n",
    "\n",
    "faiss.write_index(faiss.index_gpu_to_cpu(abs_index), 'abstracts.index')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Save the mapping from paper ID to index locally, replacing the arxivid_to_index_abs.json file.\n",
    "### 将paper id到索引的映射保存到本地，替换掉 arxivid_to_index_abs.json文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "paperid_2_index = {}\n",
    "for paper in papers_l:\n",
    "    paper_id = paper[1]['id']\n",
    "    index = paper[0]\n",
    "    paperid_2_index[paper_id] = int(index)\n",
    "with open('./paperid_to_index.json', 'w') as f:\n",
    "    json.dump(paperid_2_index, f, indent=4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Modify the file paths in the __init__ fuction within src/database.py.\n",
    "### 对src/database.py中的__init__部分的初始化文件路径做相应的修改"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
